package com.cs2340.taskplusplus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * A simple task. This class has a description, location, and categories.
 * @author GRILLENBANNER
 *
 */

public class Task implements Doable 
{
	
	protected String description, name, parentTable, labelTable, tableName;
	protected Location location;
	protected Date date;
	protected ArrayList<Label> labels;
	protected boolean isDone,isList;
	
	/**
	 * The constructor for the task. Starts with isDone false.
	 * @param name
	 * @param description
	 * @param parent
	 */
	public Task(String parent, String name, String description, Date date, List<Label> labels, Location loc)
	{
		this.name = name;
		this.description = description;
		if (parent == null)
			parent = Account.username + "_MasterList";
		this.parentTable = parent;
		this.tableName = parent + "_"+ name;
		this.isDone = false;
		this.date = date;
		this.labels = new ArrayList<Label>();
		this.labelTable = "labels_" + parent + "_" + name;
		this.location = loc;
		if (labels != null)
		{
			this.labels = (ArrayList<Label>) labels;
		}
		TaskDatabase.newTask(this);
	}
	
	/**
	 * Constructs a Task from the database. Uses parent name and task name as identifiers.
	 * @param parent
	 * @param name
	 */
	public Task(String parent, String name)
	{
		this.name = name;
		this.parentTable = parent;
		this.tableName = parent + "_"+ name;
		TaskDatabase.fillInfo(this);
	}
	
	public void delete()
	{
		TaskDatabase.delete(this);
	}
	
	/**
	 * gets task description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * sets task description
	 */
	public void setDescription(String desc) {
		this.description = desc;
	}
	
	/**
	 * get the name of the parent table
	 */
	public String getParentTable() {
		return parentTable;
	}

	/**
	 * set the name of the parent table
	 */
	public void setParentTable(String parentTable) {
		this.parentTable = parentTable;
	}


	/**
	 * gets location
	 */
	public Location getLocation() {
		return location;
	}

	/**
	 * sets location
	 */
	public void setLocation(Location loc) {
		this.location = loc;
		TaskDatabase.setLocation(this, loc);
	}

	/**
	 * check the task off
	 */
	public void setDone() 
	{
		this.isDone = true;
		TaskDatabase.setDone(this);
	}

	/**
	 * uncheck the task
	 */
	public void setNotDone() 
	{
		this.isDone = false;
		TaskDatabase.setNotDone(this);
	}

	/**
	 * gets task name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * sets task name
	 */
	public void setName(String name){
		this.name = name;
	}

	/**
	 * is the task checked off?
	 */
	public boolean isDone() {
		return isDone;
	}
	
	/**
	 * Expand this task into a list of subtasks. Return the newly create TaskList.
	 * @return
	 */
	public TaskList expand()
	{
		TaskDatabase.expand(this);
		return new TaskList(this.parentTable, this.name);
	}
	
	/**
	 * getter for labels
	 */
	public ArrayList<Label> getLabels() {
		return labels;
	}

	/**
	 * adds a label to the task.
	 * @param l
	 */
	public void addLabel(Label l) 
	{
		if (!hasLabel(l))
		{
			labels.add(l);
			TaskDatabase.addLabel(this, l);
		}
	}

	/**
	 * removes the label from the task.
	 * @param l
	 */
	public void removeLabel(Label l) 
	{
		if (hasLabel(l))
		{
			labels.remove(l);
			TaskDatabase.removeLabel(this, l);
		}
	}

	/**
	 * checks to see if the task has the applied label
	 * @param l
	 */
	public boolean hasLabel(Label l) {
		return labels.contains(l);
	}

	
	public Date getDate() {
		return date;
	}

	public void setDate(Date d) 
	{
		this.date = d;
		TaskDatabase.setDate(this,d);
	}

	public boolean isAfter(Date d) {
		return date.after(d);
	}

	public boolean isOn(Date d) {
		return this.date.equals(d);
	}

	public boolean isBefore(Date d) {
		return this.date.before(d);
	}

	public String getLabelTable() {
		return this.labelTable;
	}

	public void setLabelTable(String labelTable) 
	{
		this.labelTable = labelTable;
	}
	
	public boolean isList() 
	{
		return isList;
	}

	public void setLabels(ArrayList<Label> labels) 
	{
		this.labels = labels;
		TaskDatabase.newLabels(this);
		for (Label l : labels)
		{
			TaskDatabase.addLabel(this, l);
		}
	}
	
	public String getTableName()
	{
		return tableName;
	}
}
